//
//  CYTrendViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTrendViewController.h"
#import "CYTrendView.h"
#import "CYTrendBottomView.h"
#import "CYTrendLotteryView.h"
#import "CYTime.h"
@interface CYTrendViewController ()
@property (nonatomic, strong) CYTrendBottomView *bottomView;//走势图底部视图
@property (nonatomic, strong) CYView *alphaView;//彩种选择的背景图
@property (nonatomic, strong) CYTrendLotteryView *lotteryView;//选择彩种
@property (nonatomic, strong) CYButton *titleBtn;//navigationbar的按钮，选择彩种
@property (nonatomic, copy) NSMutableArray *lotteryArr;//所有彩种名称
@property (nonatomic, strong) CYTrendView *trendView;//走势图窗口
@property (nonatomic, strong) CYTime *timer;//定时器
@end
@implementation CYTrendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.bottomView.hidden = NO;
    // Do any additional setup after loading the view.
}

- (CYTrendBottomView *)bottomView{
    if (!_bottomView) {
        WS(self)
        _bottomView = [[CYTrendBottomView alloc]initWithColor:[UIColor whiteColor] line:LineTop lineColor:ColorHex(0x888888) block:^{
            [weakSelf performSelector:@selector(back) withObject:weakSelf.navigationItem.leftBarButtonItem];
        }];
        [self.view addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(0);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, _bottomView.bounds.size.height));
        }];
    }
    return _bottomView;
}

- (CYButton *)titleBtn{
    if (!_titleBtn) {
        _titleBtn = [[CYButton alloc]initWithTitle:_lottery_name titleColor:[UIColor whiteColor] titleSize:0 Image:@"xlsjx"];
        [_titleBtn setImage:[UIImage imageNamed:@"slsjx"] forState:UIControlStateSelected];
        _titleBtn.imagePosition = RightPosition;
        [_titleBtn addTarget:self action:@selector(actionForTitleBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _titleBtn;
}

//半透明背景
- (CYView *)alphaView{
    if (!_alphaView) {
        _alphaView = [[CYView alloc]initWithColor:RGBA(38, 38, 38, 0.8)];
        [self.view addSubview:_alphaView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionForTap:)];
        [_alphaView addGestureRecognizer:tap];
        
        [_alphaView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(NAVIBAR_HEIGHT + STATUS_HEIGHT, 0.0, 0.0, 0.0));
        }];
    }
    return _alphaView;
}

//彩种选择器
- (CYTrendLotteryView *)lotteryView{
    if (!_lotteryView) {
        WS(self)
        _lotteryView = [[CYTrendLotteryView alloc]initWithColor:[UIColor whiteColor] currentTitle:_lottery_name titles:_lotteryArr block:^(NSUInteger index) {
            [weakSelf chooseType:index];
        }];
        [_alphaView addSubview:_lotteryView];
        
        [_lotteryView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_alphaView.mas_top).with.offset(0);
            make.centerX.equalTo(_alphaView);
            make.size.mas_equalTo(_lotteryView.bounds.size);
        }];
    }
    return _lotteryView;
}

//走势图
- (CYTrendView *)trendView{
    if (!_trendView) {
        WS(self)
        _trendView = [[CYTrendView alloc]initWithBlock:^(NSString *date, int endTime) {
            [weakSelf setupDate:date time:endTime];
        }];
        [self.view addSubview:_trendView];
        [_trendView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(NAVIBAR_HEIGHT + STATUS_HEIGHT, 0, _bottomView.bounds.size.height, 0));
        }];
    }
    return _trendView;
}

- (CYTime *)timer{
    if (!_timer)
        _timer = [[CYTime alloc]init];
    return _timer;
}

//彩种名字
- (void)setLottery_name:(NSString *)lottery_name{
    if (_lottery_name != lottery_name)
        _lottery_name = lottery_name;
    [self.titleBtn setTitle:_lottery_name forState:UIControlStateNormal];
    self.trendView.lottery_name = _lottery_name;
}

- (void)setLottery_id:(NSString *)lottery_id{
    if (_lottery_id != lottery_id) {
        _lottery_id = lottery_id;
        self.trendView.lottery_id = lottery_id;
    }
}

//倒计时和期数
- (void)setupDate:(NSString *)date time:(int)endTime{
    WS(self)
    _bottomView.date = date;
    [self.timer countDown:endTime time:^(NSString *time) {
        weakSelf.bottomView.time = time;
    } complete:^{
        //        [weakSelf https:NO];
    }];
}

- (void)showLotteryView{
    self.alphaView.hidden = self.lotteryView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        _lotteryView.transform = CGAffineTransformMakeTranslation(0.0, _lotteryView.bounds.size.height);
    }];
}

- (void)hiddenLotteryView{
    [UIView animateWithDuration:0.3 animations:^{
        _lotteryView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        _alphaView.hidden = YES;
    }];
}

/*弹出选择票种视图*/
- (void)actionForTitleBtn:(CYButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected)
        [self showLotteryView];
    else
        [self hiddenLotteryView];
}

//点击事件
- (void)actionForTap:(UITapGestureRecognizer *)tap{
    [self hiddenLotteryView];
    [self performSelector:@selector(actionForTitleBtn:) withObject:_titleBtn];
}

/*选择票种*/
- (void)chooseType:(NSUInteger )index{
    _alphaView.hidden = YES;
    self.lottery_id = _lotteryArr[index][@"id"];
    self.lottery_name = _lotteryArr[index][@"name"];
    self.titleBtn.selected = NO;
}

- (void)back{
    //判断是否从buyviewcontroller进入，如果是，不传值
    if ([_previousVC isEqualToString:@"CYBuyViewController"])
        [self popPreviousViewController];
    else
        [self backWithValues:@[_lottery_id, _lottery_name] keys:@[@"lottery_id",@"lottery_name"]];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.lotteryArr = [CYCache getCache:@"LotteryName.plist"];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.timer cancel];
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.titleView = self.titleBtn;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
